package com.cbhb.sys;

import java.util.Stack;

/**
 * 给出一个仅包含字符'(',')','{','}','['和']',的字符串，判断给出的字符串是否是合法的括号序列
 * 括号必须以正确的顺序关闭，"()"和"()[]{}"都是合法的括号序列，但"(]"和"([)]"不合法。
 */
public class IsValid {

    public boolean isValid(String s) {
        // write code here
        Stack<Character> stack = new Stack<Character>();
        for(int i=0;i<s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '[' || ch == '{' || ch == '(') {
                stack.push(ch);
            } else {
                if(stack.size()==0) return false;
                else if(stack.peek() == '(' && ch == ')') stack.pop();
                else if(stack.peek() == '[' && ch == ']') stack.pop();
                else if(stack.peek() == '{' && ch == '}') stack.pop();
            }
        }
        return stack.size() ==0;
    }

    public static void main(String[] args) {
        System.out.println(new IsValid().isValid("}"));
    }
}
